<template>
  <a-modal
    :open="open"
    width="640px"
    :title="rowData?.thdh ? '修改退货单' : '新增退货单'"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="退货仓库" class="w-1/2" v-bind="validateInfos.thckmc">
        <SelectWarehouse v-model:value="formState.thckmc" @select="onSelectWarehouse" />
      </a-form-item>
      <a-form-item label="退货日期" class="w-1/2" v-bind="validateInfos.thsj">
        <a-date-picker v-model:value="formState.thsj" class="w-full" placeholder="退货日期" :allow-clear="false" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="供应商" class="w-full" v-bind="validateInfos.gysmc">
        <SelectTable v-model:value="formState.gysmc" type="供应商" @select="onSelectSupplier" />
      </a-form-item>
      <a-form-item label="发票状态" class="w-1/2">
        <a-radio-group v-model:value="formState.fpzt">
          <a-radio :value="0">普通</a-radio>
          <a-radio :value="1">在途</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="发票号码" class="w-1/2" v-bind="validateInfos.fph">
        <a-input v-model:value="formState.fph" :disabled="formState.fpzt" placeholder="发票号码" />
      </a-form-item>
      <a-form-item label="发票日期" class="w-1/2" v-bind="validateInfos.fprq">
        <a-date-picker v-model:value="formState.fprq" :disabled="formState.fpzt" value-format="YYYY-MM-DD" class="w-full" placeholder="发票日期" />
      </a-form-item>
      <a-form-item label="制单人:" class="w-1/2">
        {{ empName }}
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.cmemo" :rows="1" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import SelectWarehouse from '@/components/SelectWarehouse'
import useUserStore from '@/store/user'
import { post } from '@/utils/request'

const router = useRouter()
const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  thckmc: '',
  thsj: '',
  gysmc: '',
  fpzt: 0,
  fph: '',
  fprq: ''
})
const rulesState = ref({
  thckmc: [{ required: true, message: '请选择退货仓库' }],
  gysmc: [{ required: true, message: '请选择供应商' }],
  thsj: [{ required: true, message: '请选择退货时间' }],
  fph: [{ required: formState.value.fpzt ? true : false, message: '请选择发票号码' }],
  fprq: [{ required: formState.value.fpzt ? true : false, message: '请选择发票日期' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

const { empName, empId } = useUserStore()
watchEffect(() => {
  if (props.open) {
    formState.value = { fpzt: 0, thr: empId, empName, thsj: dayjs().format('YYYY-MM-DD HH:mm:ss'), ...props.rowData }
  }
})
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()

  if (formState.value?.thsj && formState.value?.thckbh) {
    delete formState.value.createUser
    const { code, data } = await post('/drugStorage/psiform/doSaveReturnOrder', formState.value)
    if (code === 0) {
      if (!props.rowData?.thckbh) {
        router.push({
          path: '/drug-trace/warehouse/return-detail',
          query: {
            thdh: data.thdh
          }
        })
      }
      message.success('保存成功')
      onCancel()
      emit('ok')
    }
  }
}
const onSelectWarehouse = (_, supplier) => {
  formState.value.thckbh = supplier?.ckid
  formState.value.thckmc = supplier?.ckmc
}
const onSelectSupplier = (supplier) => {
  formState.value.gysbh = supplier?.gysbh
  formState.value.gysmc = supplier?.gysmc
}
</script>
<style lang="less" scoped></style>
